<!-- 扫码登录 -->
<template>
  <div class="execute">
    <div class="execute_content">
      <loginHeader class="page_header" ref="loginHeader" :isBack="true"></loginHeader>

      <div class="code-img" v-if="buttonShow">
        <div style="width: 100%;text-align: center;padding-top: 15%;">
          <img style="width:5.5rem;height: 5.5rem;border: none;" src="../../../static/img/invalid_code.png" />
          <p style="font-size: 0.5rem;color: white;margin-top: 10px;">二维码已失效</p>
          <P style="font-size: 0.428rem;color: #376BE3;">请返回重新扫描</P>
        </div>
      </div>
      <div class="code-img" v-else>
        <div style="width: 100%;text-align: center;padding-top: 15%;">
          <img style="width:5.5rem;height: 3.5rem;border: none;" src="../../../static/img/comfirm_login.png" />
          <p style="font-size: 0.5rem;color: white;margin-top: 10px;">IU工业云确认登录</p>
        </div>
      </div>

      <div v-if="buttonShow">
        <button class="submit" @click="submit">确认</button>
        <button class="submit-cancel" @click="redirect">取消登录</button>
      </div>
      <div v-else>
        <button class="submit" @click="submit">确认登录</button>
        <button class="submit-cancel" @click="redirect">取消登录</button>
      </div>
    </div>
  </div>
</template>

<script>
  import loginHeader from '@/components/common/loginHeader'
  import scan_dao from '@/daos/scan_dao.js'
  import host from '../../config/host';

  export default {
    components: {
      loginHeader
    },
    computed: {},
    data() {
      return {
        buttonShow: true,
        appSign: '',
      }
    },
    watch: {},
    created() {},
    mounted() {
      this.$http(this.$API.getUserInfo, {}, true, false, false).then((res) => {
        if (!res) {
          this.redirect()
        } else {
          let params = this.getUrlKey('appSign')
          let url = host.pcHostUrl + '/wx/app/scanCode.json?appSign=' + params
          this.$http(url, {}, true).then((res) => {
            this.appSign = params
            this.buttonShow = !res
          })
        }
      })
    },
    methods: {
      //传入url参数名获取参数值
      getUrlKey: function(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
      },
      //登录:true时二维码过期,false时登录等待确认
      submit: function() {
        if (this.buttonShow) {
          this.redirect()
        } else {
          let params = this.appSign
          let url = host.pcHostUrl + '/wx/app/confirmLogin.json?appSign=' + params
          this.$http(url, {}, true).then((res) => {
            if (res) {
              this.redirect()
            }
          })
        }
      },
      //跳转登录页面
      redirect: function() {
        window.location.href = host.wxSsoUrl
      },
    }
  }
</script>

<style lang="less" scoped>
  .execute {
    background: #22233F;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    overflow-y: auto;

    .code-img {
      width: 100%;
      height: 9.5rem;
      border: none;
    }

    .submit {
      display: block;
      width: 92%;
      margin-left: 4%;
      margin-top: 20px;
      border: none;
      outline: none;
      background: #456DE6;
      font-size: 17px;
      color: #fff;
      line-height: 24px;
      padding: 10px 0 10px;
      text-align: center;
      border-radius: 4px;
    }

    .submit-cancel {
      display: block;
      width: 92%;
      margin-left: 4%;
      margin-top: 5px;
      border: none;
      outline: none;
      background: #22233F;
      font-size: 17px;
      color: #fff;
      line-height: 24px;
      padding: 10px 0 10px;
      text-align: center;
      border-radius: 4px;
    }
  }

</style>
